﻿using DailyApp.WPF.HttpClients;
using DailyApp.WPF.ViewModels;
using DailyApp.WPF.ViewModels.DialogViewModels;
using DailyApp.WPF.Views;
using DailyApp.WPF.Views.Dialogs;
using DryIoc;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System.Configuration;
using System.Data;
using System.Windows;

namespace DailyApp.WPF
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        /// <summary>
        /// 初始化启动页面
        /// </summary>
        /// <returns></returns>
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }


        //依赖注入
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //注册登陆对话框
            containerRegistry.RegisterDialog<LoginUC, LoginUCViewModel>();
            containerRegistry.RegisterForNavigation<AddWaitUC, AddWaitUCViewModel>();//添加待办事项--仅仅通过导航辅助注入，但未绑定对话框，因此在自定义服务中进行了判断绑定。
            containerRegistry.RegisterForNavigation<ModifyWaitUC, ModifyWaitUCViewModel>();
            containerRegistry.RegisterForNavigation<AddMemoUC, AddMemoUCViewModel>();
            containerRegistry.RegisterForNavigation<ModifyMemoUC, ModifyMemoUCViewModel>();

            //依赖注入手动绑定绑定视图模型
            //containerRegistry.RegisterForNavigation<MainWindow, MainWindowViewModel>();
            //ViewModelLocationProvider.Register<MainWindow, MainWindowViewModel>();

            //(首页)依赖注入导航页并手动绑定视图模型
            containerRegistry.RegisterForNavigation<HomeUC, HomeUCViewModel>();//首页
            containerRegistry.RegisterForNavigation<WaitUC, WaitUCViewModel>();//待办事项
            containerRegistry.RegisterForNavigation<MemoUC, MemoUCviewModel>();//备忘录
            containerRegistry.RegisterForNavigation<SettingsUC, SettingsUCViewModel>();//设置



            //(设置页)依赖注入
            containerRegistry.RegisterForNavigation<PersonalUC, PersonalUCViewModel>();//个性化页面


            //注入请求api服务
            containerRegistry.GetContainer().Register<HttpRestClient>(made: Parameters.Of.Type<string>(serviceKey: "webUrl"));
        }

        /// <summary>
        /// 重写初始化的方法
        /// </summary>
        protected override void OnInitialized()
        {
            //实例化一个对话框服务
            var dialog = Container.Resolve<IDialogService>();
            //ShowDialog这个方法会阻塞程序继续向下运行，直到窗体执行回调函数。
            dialog.ShowDialog("LoginUC", callback =>
            {
                if (callback.Result != ButtonResult.OK)
                {
                    //如果不是则意味着用户可能点击了关闭按钮，要关闭主窗体
                    Environment.Exit(0);
                    return;
                }

                /* 
                 * 这里的处理很类似回调函数
                 */
                //默认跳转首页                
                if (Current.MainWindow.DataContext is MainWindowViewModel mainVm)
                    mainVm.InitNavigation(callback.Parameters.GetValue<string>("LoginName"));

                base.OnInitialized();

            });
        }
    }

}
